package com.tan.user.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tan.common.domain.BaseModel;
import com.tan.common.domain.user.SysDict;
import com.tan.common.domain.user.SysDictType;
import com.tan.common.domain.user.SysMenu;
import com.tan.common.dto.user.DictTypeCreateDTO;
import com.tan.common.dto.user.DictTypeUpdateDTO;
import com.tan.common.mapper.user.SysDictTypeMapper;
import com.tan.common.mapstruct.user.DictTypeConvert;
import com.tan.common.mapstruct.user.MenuConvert;
import com.tan.common.query.user.DictTypeQuery;
import com.tan.common.query.user.MenuQuery;
import com.tan.common.tools.ConvertTool;
import com.tan.common.tools.PageTool;
import com.tan.common.vo.user.DictTypeVO;
import com.tan.common.vo.user.MenuVO;
import com.tan.core.utils.ExceptionTool;
import com.tan.core.utils.StringUtils;
import com.tan.user.service.ISysDictTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author Tan
 * @since 2025-01-27
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {


    @Override
    public void createDictType(DictTypeCreateDTO dictTypeCreateDTO) {
        SysDictType dictType = DictTypeConvert.INSTANCE.dtoDomain(dictTypeCreateDTO);
        SysDictType type = this.baseMapper.selectOne(Wrappers.lambdaQuery(SysDictType.class)
                .eq(SysDictType::getCode, dictTypeCreateDTO.getCode()));
        ExceptionTool.systemError(type != null, "字典code已存在");
        int count = this.baseMapper.insert(dictType);
        ExceptionTool.operateSqlError(count, "创建字典类型失败");
    }


    @Override
    public void updateDictType(DictTypeUpdateDTO dictTypeUpdateDTO) {
        SysDictType dictType = DictTypeConvert.INSTANCE.dtoDomain(dictTypeUpdateDTO);
        if (StringUtils.isNotNull(dictTypeUpdateDTO, DictTypeCreateDTO::getCode)) {
            SysDictType type = this.baseMapper.selectOne(Wrappers.lambdaQuery(SysDictType.class)
                    .eq(SysDictType::getCode, dictTypeUpdateDTO.getCode())
                    .ne(SysDictType::getId, dictTypeUpdateDTO.getId()));
            ExceptionTool.systemError(type != null, "code已存在");
        }
        int count = this.baseMapper.updateById(dictType);
        ExceptionTool.operateSqlError(count, "创建字典类型失败");
    }

    @Override
    public List<SysDictType> dictTypes() {
        return this.baseMapper.selectList(Wrappers.lambdaQuery(SysDictType.class).eq(SysDictType::getStatus, true));
    }

    @Override
    public List<DictTypeVO> dictTypeVOS() {
        return DictTypeConvert.INSTANCE.toTargets(this.dictTypes());
    }

    @Override
    public void delDict(Long dictId) {
        SysDictType dictType = this.baseMapper.selectById(dictId);
        ExceptionTool.isNullError(dictType, "字典类型不存在");
        int count = this.baseMapper.deleteById(dictId);
        ExceptionTool.operateSqlError(count, "删除字典类型失败");
    }

    @Override
    public IPage<DictTypeVO> dictTypePages(DictTypeQuery query) {
        LambdaQueryWrapper<SysDictType> wrapper = Wrappers.lambdaQuery(SysDictType.class);
        wrapper.eq(query.getCode() != null, SysDictType::getCode, query.getCode());
        wrapper.like(com.tan.core.utils.StringUtils.isNotBlank(query.getName()), SysDictType::getName, query.getName());
        wrapper.orderByDesc(SysDictType::getId);
        IPage<SysDictType> pages = this.baseMapper.selectPage(new Page<>(query.getCurrent(), query.getSize()), wrapper);
        return PageTool.convert(pages, DictTypeConvert.class);
    }
}
